import {
    GetUserInfo
} from './getUserInfo-model.js'
const $http = new GetUserInfo()
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {},
        status: true,
        isShowGetPhone: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options) {
            this.setData({
                options
            })
        }
        wx.checkSession({
            success(res) {
                console.log(res)
            },
            fail(res) {
                console.log('fail', res)
            }
        })
    },
    getUserInfo(e) {
        console.log(e.detail)
        if (e.detail.errMsg === 'getUserInfo:ok') {
            console.log('获取用户信息成功')
            wx.showLoading({
                title: '加载中...',
            })

            app._loginUser(e.detail, () => {
                wx.hideLoading()
                if (wx.getStorageSync('phone') == '') {
                    console.log('没有绑定手机号')
                    this.setData({
                        isShowGetPhone: true
                    })
                } else {
                    console.log('绑定了手机号')
                    this.limitLogin(this);
                }
            })
        } else {
            console.log('fail', '获取用户信息失败')
            wx.showModal({
                title: '提示',
                content: '获取用户信息失败',
                showCancel: false,
                confirmColor: '#e2211c',
                success(res) {

                }
            })
            // app.showTips(() => {
            //     wx.hideLoading()
            //     //分享进入跳转到分享页面
            //     if (wx.getStorageSync('phone') == '') {
            //         console.log('没有绑定手机号')
            //         this.setData({
            //             isShowGetPhone: true
            //         })
            //     } else {
            //         console.log('绑定了手机号')
            //         this.limitLogin(this);
            //     }
            // })
        }
    },
    getPhoneNumber(e) {
        console.log(e.detail)

        if (e.detail.errMsg == "getPhoneNumber:ok") {
            console.log('同意获取手机号')
            let params = {
                encryptedData: e.detail.encryptedData,
                iv: e.detail.iv
            }
            $http.pullPhone(params, res => {
                if (res.status) {
                    console.log('获取手机号成功')
                    wx.setStorageSync('phone', res.data.purePhoneNumber)
                    this.limitLogin(this);

                } else {
                    console.log('获取手机号失败')
                    wx.showToast({
                        icon: 'none',
                        title: res.message || '服务器错误',
                    })
                }
            })
        } else {
            console.log('不同意获取手机号')
            this.limitLogin(this);
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },
    limitLogin: function (that) {

        //分享进入跳转到分享页面
        console.log('type--------------', that.data.pageType);

        let path = that.data.options.path;
        console.log('path===================', path);

        if (path) {
            //   wx.removeStorageSync('inviteCode');
            let options = that.data.options;

            let params = "";
            console.log("options--------------------", options)
            for (var variable in options) { //variable 为属性名
                console.log("variable----------", variable);
                if (variable != 'path') {
                    params += variable + "=" + options[variable] + '&'
                }
            }
            console.log("跳转--------", path + '?' + params, path)
          if (path == "/pages/user/user" || path == "/pages/cart/cart" ) {
                wx.switchTab({
                    url: params == '' ? path : path + '?' + params,
                })
            } else {
                wx.redirectTo({
                    url: params == '' ? path : path + '?' + params,
                })
            }

        } else if (!that.data.pageType) {
            wx.switchTab({
                url: '/pages/index/index',
            })
        } else {
            //   wx.removeStorageSync('inviteCode');
            // wx.setStorageSync('pageType', 'share') //分享进入页面标识
            wx.navigateBack();
        }
    }


})